/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: dog_micro_service.cpp
 *
 * Purpose: micro_service base class implementation
 *
 * Developer:
 *   wen.gu , 2020-08-19
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#define LOG_TAG "Mics"
#include "service/dog_micro_service.h"


#include "dog_log.h"
namespace dog
{

/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/

class MicroService::impl
{
public:
    ServiceMethodHandler mMthodHandler;
    SergiceSignalHandler mSignalHandler;
};

/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/

MicroService::MicroService(const std::string& name, const std::string& version)
    :Component(name, version),
    mImpl(new impl)
{
    /** todo, something */
}

MicroService::~MicroService()
{
    /** todo, something */
}

void MicroService::setMethodHandler(ServiceMethodHandler  handler)
{
    mImpl->mMthodHandler = handler;
}

void MicroService::setSignalHandler(SergiceSignalHandler handler)
{
    mImpl->mSignalHandler = handler;
}

DogErrc MicroService::start()
{  
    /** todo, something */
    return DogErrc::OK;
}

DogErrc MicroService::stop()
{
    /** todo, something */
    return DogErrc::OK;
}

 /** use only for sub class( micro service component)*/
DogErrc MicroService::registerMethod(ServiceMethodMap& methodMap, const std::string& name, ServiceMethod method)
{
    if (name.empty() || (method == nullptr)) return DogErrc::BadParameter;

    ServiceMethodMap::iterator it = methodMap.find(name);

    if (it == methodMap.end())
    {
        methodMap[name] = method;
        return DogErrc::OK;
    }
    else
    {
        LOGE("method(%s) register by other srv, srv(%s) cann't overwrite it\n",  name.c_str(), mCompName.c_str());
    }

    return DogErrc::AlreadyExisted;
}

    /** async */
void MicroService::emitSignal(const std::string& name, const Value& value)
{
    if (mImpl->mSignalHandler)
    {
        mImpl->mSignalHandler(name, value);
    }
}

    /** do method call between micro services register in same container */
DogErrc MicroService::invoke(const std::string& strMethod, const Value& param, Value& reply)
{
    if (mImpl->mMthodHandler)
    {
        return mImpl->mMthodHandler(strMethod, param, reply);
    }

    return DogErrc::NoInit;
}

} /** namespace dog */
